package com.service.abolishing.controller;

import com.service.abolishing.dto.FishingInfoDto;
import com.service.abolishing.entity.FishingInfo;
import com.service.abolishing.service.FishingInfoService;
import com.service.config.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.service.config.constant.Constant.*;

/**
 * 渔港信息表
 * (FishingInfo)表控制层
 *
 * @author makejava
 * @since 2021-10-21 14:14:34
 */
@RestController
@RequestMapping("fishingInfo")
@Api(value = "渔港信息接口", tags = "渔港信息接口")
public class FishingInfoController {
    /**
     * 服务对象
     */
    @Resource
    private FishingInfoService fishingInfoService;

    @Resource
    private RedisUtil redisUtil;

    /**
     * 查询渔港信息
     * @param cloudAuthToken
     * @return
     */
    @GetMapping("getFishingInfo")
    @ApiOperation(value = "查询渔港信息")
    @ResponseBody
    public Result getFishingInfo(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                FishingInfo fishingInfo = new FishingInfo();
                fishingInfo.setManageInfoId(userId);
                List<FishingInfo> envFactors = fishingInfoService.queryAll(fishingInfo);
                return Result.ok().setData(envFactors);
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    /**
     * 修改渔港信息
     * @param cloudAuthToken
     * @return
     */
    @PostMapping("freshFishingInfo")
    @ApiOperation(value = "修改渔港信息")
    @ResponseBody
    public Result freshFishingInfo(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                 @RequestBody FishingInfoDto fishingInfoDto) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                FishingInfo fishingInfo = ModelMapperUtil.strictMap(fishingInfoDto,FishingInfo.class);
                fishingInfo.setManageInfoId(userId);
                fishingInfoService.update(fishingInfo);
                return Result.ok();
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    /**
     * 新增渔港信息
     * @param cloudAuthToken
     * @return
     */
    @PostMapping("addFishingInfo")
    @ApiOperation(value = "新增渔港信息")
    @ResponseBody
    public Result addFishingInfo(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                               @RequestBody FishingInfoDto fishingInfoDto) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                FishingInfo fishingInfo = ModelMapperUtil.strictMap(fishingInfoDto,FishingInfo.class);
                fishingInfo.setManageInfoId(userId);
                fishingInfo.setId(IdUtil.getStringId());
                fishingInfoService.insert(fishingInfo);
                return Result.ok();
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    /**
     * 删除渔港信息
     * @param cloudAuthToken
     * @return
     */
    @PostMapping("delFishingInfo")
    @ApiOperation(value = "删除渔港信息")
    @ResponseBody
    public Result delFishingInfo(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                               @RequestParam String fId) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                fishingInfoService.deleteById(fId);
                return Result.ok();
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
}
